local skels = {}

Fk:loadTranslationTable {
  ["lb__zaiyun"] = "灾孕",
  [":lb__zaiyun"] = "当你受到伤害后，你可以摸两张牌，然后你可以将一张牌置于武将牌上，称为「<b>卵</b>」。准备阶段，你可以将所有「<b>卵</b>」当目标数至多为选择「<b>卵</b>」数的【南蛮入侵】使用。",

  ["#lb__zaiyun"] = "灾孕：你可以将所有「<b>卵</b>」当至多指定%arg个目标的【南蛮入侵】使用",
  ["#lb__zaiyun_darw"] = "灾孕：你可以摸两张牌，然后你可以将一张牌置于武将牌上，称为「<b>卵</b>」",
  ["#lb__zaiyun_put"] = "灾孕：你可以将一张牌置于武将牌上，称为「<b>卵</b>」",

  ["lb__zaiyun__luan"] = "卵",

  ["$lb__zaiyun1"] = "",
  ["$lb__zaiyun2"] = "",
}

for i = 0, 1, 1 do
  local zaiyun = fk.CreateSkill({
    name = i == 0 and "lb__zaiyun" or "lb__zaiyun__" .. i,
  })

  Fk:loadTranslationTable {
    ["lb__zaiyun__" .. i] = "灾孕",
    [":lb__zaiyun__" .. i] = "当你受到伤害后，你可以摸两张牌，然后你可以将一张牌置于武将牌上，称为「<b>卵</b>」。准备阶段，你可以将所有「<b>卵</b>」当目标数至多为选择「<b>卵</b>」数的【南蛮入侵】使用。",
  }

  zaiyun:addEffect(fk.Damaged, {
    mute = true,
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(zaiyun.name)
    end,
    on_cost = function(self, event, target, player, data)
      return player.room:askToSkillInvoke(player, { skill_name = "lb__zaiyun", prompt = "#lb__zaiyun_darw" })
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      room:notifySkillInvoked(player, "lb__zaiyun", "drawcard")
      player:broadcastSkillInvoke("lb__zaiyun")
      player:drawCards(2, "lb__zaiyun")
      if not player:isNude() then
        local cards = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          skill_name = "lb__zaiyun",
          cancelable = true,
          include_equip = true,
          prompt = "#lb__zaiyun_put"
        })
        if #cards > 0 then
          player:addToPile("lb__zaiyun__luan", cards, true, "lb__zaiyun", player)
        end
      end
    end,
  })

  zaiyun:addEffect(fk.EventPhaseStart, {
    mute = true,
    can_trigger = function(self, event, target, player, data)
      local cards = player:getPile("lb__zaiyun__luan")
      if target == player and player:hasSkill(zaiyun.name) and #cards > 0 then
        local card = Fk:cloneCard("savage_assault")
        card:addSubcards(cards)
        card.skillName = "lb__zaiyun"
        return table.find(player.room:getOtherPlayers(player), function(to)
          return player:canUseTo(card, to)
        end)
      end
    end,
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local cards = player:getPile("lb__zaiyun__luan")
      local card = Fk:cloneCard("savage_assault")
      card:addSubcards(cards)
      card.skillName = "lb__zaiyun"
      local tos = table.filter(player.room:getOtherPlayers(player), function(to)
        return player:canUseTo(card, to)
      end)
      local num = math.min(#cards, #tos)
      tos = room:askToChoosePlayers(player, {
        targets = tos,
        max_num = num,
        min_num = 1,
        skill_name = "lb__zaiyun",
        cancelable = true,
        prompt = "#lb__zaiyun:::" .. num,
      })
      if #tos > 0 then
        room:sortByAction(tos)
        event:setCostData(self, { tos = tos })
        return true
      end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local tos = event:getCostData(self).tos
      room:notifySkillInvoked(player, "lb__zaiyun", "offensive", tos)
      player:broadcastSkillInvoke("lb__zaiyun")
      player.room:useVirtualCard("savage_assault", player:getPile("lb__zaiyun__luan"), player, tos, "lb__zaiyun")
    end,
  })

  table.insert(skels, zaiyun)
end

return skels